package com.zpark.util;

import com.zpark.entity.EvaluateData;
import com.zpark.entity.GeoPoint;
import com.zpark.entity.SuccessData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 把接收到的日志字符串，转换成需要的对象
 */
public class LogUtil {
    /**
     * 评估数据的正则表达式
     */
    private static final String EVALUATE_REGEX = "^INFO\\s(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})\\s([a-z0-9\\u4e00-\\u9fa5]*)\\s(EVALUATE)\\s\\[([a-z0-9\\u4e00-\\u9fa5]*)\\]\\s([a-z0-9]{32})\\s\\\"([a-z0-9\\.\\-\\,]{6,12})\\\"\\s([a-z\\u4e00-\\u9fa5]*)\\s\\\"([0-9\\.\\,]*)\\\"\\s\\[([0-9\\,\\.]*)\\]\\s\\\"(.*)\\\"";

    /**
     * 登录成功数据的正则表达式
     */
    private static final String SUCCESS_REGEX = "^INFO\\s(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})\\s([a-z0-9\\u4e00-\\u9fa5]*)\\s(SUCCESS)\\s\\[([a-z0-9\\u4e00-\\u9fa5]*)\\]\\s([a-z0-9]{32})\\s\\\"([a-z0-9\\.\\-\\,]{6,12})\\\"\\s([a-z\\u4e00-\\u9fa5]*)\\s\\\"([0-9\\.\\,]*)\\\"\\s\\[([0-9\\,\\.]*)\\]\\s\\\"(.*)\\\"";

    //评估数据的pattern
    private static final Pattern EVALUATE_PATTERN = Pattern.compile(EVALUATE_REGEX, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    //登录成功数据的pattern
    private static final Pattern SUCCESS_PATTERN = Pattern.compile(SUCCESS_REGEX, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    /**
     * 判断过来的日志信息，是否是评估数据
     * @param log
     * @return
     */
    public static boolean isEvaluate(String log){
        Matcher matcher = EVALUATE_PATTERN.matcher(log);
        return matcher.find();
    }

    /**
     * （这个方法不应该要）
     * 判断过来的日志信息，是否是登录成功数据
     * @param log
     * @return
     */
    @Deprecated
    public static boolean isSuccess(String log){
        Matcher matcher = SUCCESS_PATTERN.matcher(log);
        return matcher.find();
    }

    /**
     * 把日志信息解析为评估对象
     * @param log
     * @return
     */
    public static EvaluateData parseLog2EvaluateData(String log) throws ParseException {
        if(isEvaluate(log)){
            //日志是评估数据
            Matcher matcher = EVALUATE_PATTERN.matcher(log);
            matcher.find();
            /*for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }*/

            //group1的处理
            Date loginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(matcher.group(1));
            long evaluateTime = loginDate.getTime();

            //group8的处理
            String[] jingWeiDu = matcher.group(8).split(",");
            GeoPoint geoPoint = new GeoPoint(Double.valueOf(jingWeiDu[0]),Double.valueOf(jingWeiDu[1]));

            //group9的处理
            String[] inputFeaturesStr = matcher.group(9).split(",");
            Double[] inputFeatures=new Double[inputFeaturesStr.length];
            for (int i = 0; i < inputFeaturesStr.length; i++) {
                inputFeatures[i]=Double.parseDouble(inputFeaturesStr[i]);
            }

            return new EvaluateData(evaluateTime,matcher.group(2),matcher.group(4),matcher.group(5),matcher.group(6),matcher.group(7),geoPoint,inputFeatures,matcher.group(10));
        }
        return null;
    }

    /**
     * 把日志信息解析为登录成功对象
     * @param log
     * @return
     */
    public static SuccessData parseLog2SuccessData(String log) throws ParseException {
        if(isSuccess(log)){
            //日志是评估数据
            Matcher matcher = SUCCESS_PATTERN.matcher(log);
            matcher.find();
//            System.out.println(matcher.groupCount()+"***");
            /*for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }*/

            //group1的处理
            Date loginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(matcher.group(1));
            long evaluateTime = loginDate.getTime();

            //group8的处理
            String[] jingWeiDu = matcher.group(8).split(",");
            GeoPoint geoPoint = new GeoPoint(Double.valueOf(jingWeiDu[0]),Double.valueOf(jingWeiDu[1]));

            //group9的处理
            String[] inputFeaturesStr = matcher.group(9).split(",");
            Double[] inputFeatures=new Double[inputFeaturesStr.length];
            for (int i = 0; i < inputFeaturesStr.length; i++) {
                inputFeatures[i]=Double.parseDouble(inputFeaturesStr[i]);
            }

            return new SuccessData(evaluateTime,matcher.group(2),matcher.group(4),matcher.group(5),matcher.group(6),matcher.group(7),geoPoint,inputFeatures,matcher.group(10));
        }
        return null;
    }

    /**
     * 根据日志信息，获取到应用名
     * @param log
     * @return
     */
    public static String getApplicationName(String log){
        Matcher evaluateMatcher = EVALUATE_PATTERN.matcher(log);
        if(evaluateMatcher.find()){
            return evaluateMatcher.group(2);
        }
        Matcher successMatcher = SUCCESS_PATTERN.matcher(log);
        if(successMatcher.find()){
            return successMatcher.group(2);
        }

        return null;
    }

    /**
     * 根据日志信息，获取到用户名
     * @param log
     * @return
     */
    public static String getUserIdentify(String log){
        Matcher evaluateMatcher = EVALUATE_PATTERN.matcher(log);
        if(evaluateMatcher.find()){
            return evaluateMatcher.group(4);
        }
        Matcher successMatcher = SUCCESS_PATTERN.matcher(log);
        if(successMatcher.find()){
            return successMatcher.group(4);
        }
        return null;
    }


    /**
     * 可以从日志信息中提取所有数据
     * @param log 日志信息
     * @param index matcher中的group索引
     * @return 提取的其中一个数据
     */
    public static String getOneInformation(String log,int index){

        Matcher evaluateMatcher = EVALUATE_PATTERN.matcher(log);
        if(evaluateMatcher.find()){
            return evaluateMatcher.group(index);
        }
        Matcher successMatcher = SUCCESS_PATTERN.matcher(log);
        if(successMatcher.find()){
            return successMatcher.group(index);
        }
        return null;
    }


}
